// src/stores/classStore.ts
import { defineStore } from 'pinia';

import type { ClassInfo } from '@/api/class/type';
import { reqClassGetClasses } from '@/api/class';


let useClassStore = defineStore('ClassStore', {
    state: () => ({
        classes: [] as ClassInfo[], // 存储班级信息
        loading: false,            // 加载状态
        error: null as string | null, // 错误信息
    }),
    actions: {
        async fetchClasses() {
            console.log("获取班级信息没得");
            this.loading = true;
            try {
                const response = await reqClassGetClasses();
                console.log("看下接口有没有正确返回")
                console.log(response);
                console.log("看下接口有没有正确返回")
                if (response.code == 200) {
                    console.log(response.data)
                    console.log("上面是班级信息")
                    this.classes = response.data;
                    console.log("下是班级信息")
                    console.log(this.classes);
                    this.error = null;
                    return 'ok';
                } else {
                    return Promise.reject(new Error(response.message || "获取班级信息失败"));
                }

            } catch (err) {
                console.error('Error fetching classes:', err);
                // 使用类型断言
                const error = err as any;
                this.error = error.response?.data?.message || 'Failed to fetch classes';
            } finally {
                this.loading = false;
            }
        },

    },
    getters: {

    }
});
export default useClassStore